﻿using System.Threading;
using System.Collections.Generic;

namespace BMSPlatform.DeviceCommunication
{
    /// <summary>
    /// 上位机设备通讯层管理
    /// </summary>
    public class Device
    {
        private const int MaxThreadPoolNum = 5;//最大线程数
        public static List<Thread> DataFilterThreads;//数据筛选
        /// <summary>
        /// 链接成功后，初始化设置
        /// </summary>
        public static void DeviceInit()
        {//多线程处理
            DataFilterThreads = new List<Thread>();
            Thread dataHandleThread;
            for (int i = 0; i < MaxThreadPoolNum; i++)
            {
                dataHandleThread = new Thread(DeviceDataHandle.ReceivedDataQueueHandle);
                dataHandleThread.IsBackground = true;
                dataHandleThread.Start();
                DataFilterThreads.Add(dataHandleThread);
                //new Thread(DeviceDataHandle.ReceivedDataQueueHandle) { IsBackground = true }.Start();
            }
        }

        public static void DeviceClose()
        {//终止线程，释放资源
            try
            {
                if (DataFilterThreads?.Count > 0)
                {
                    foreach (Thread thread in DataFilterThreads)
                    {
                        thread.Abort();
                    }
                }
            }
            catch { }
           
        }
        #region 命令下发
        
        #endregion
    }
}
